/* eslint-disable no-loss-of-precision */
// eslint 数据精度严格检查规则,误删

import { all, create } from 'mathjs';

/**
 * @param config 基础配置,具体请查询 https://mathjs.org/docs/index.html
 */
const config: any = {
  number: 'BigNumber', // Default type of number:
  // 'number' (default), 'BigNumber', or 'Fraction'
  precision: 64, // Number of significant digits for BigNumbers
  epsilon: 1e-60,
};
const math = create(all, config);

/**
 * @exports precisionFilter 精度过滤器
 * @param equation 传入的计算公式,string型 eg: "x+y"  | "x-y" | "(x+y)*y/y"
 * @param value 配合equation 使用,传入对应的值 eg: { x:1,y:1 }
 * @returns 过滤后的值
 * precision=14
 */
export const precisionFilter = (equation: string, value: object) => {
  return math.format(math.evaluate(`${equation}`, value), { notation: 'fixed', precision: 2 });
};

/**
 * @exports precisionFilteDisplace  如果precisionFilter 方法出现精度问题,请用此方法替代(以防万一)
 * @param equation 传入的计算公式,string型 eg: "1+1"  | "1-1" | "(1+1)*1/1"
 * @returns 过滤后的值
 */
export const precisionFilteDisplace = (equation: string) => {
  return math.format(math.evaluate(equation));
};

/**
 * @exports BGNBTransForm 单数字过滤器
 * @param param 单一数字
 * @returns
 */
export const BGNBTransForm = (param: number) => {
  return math.format(math.bignumber(param));
};
